<?php

require_once dirname(__FILE__).'/../../../../test/bootstrap.php';
require_once dirname(__FILE__).'/../../../../lib/urCronServiceBase.class.php';
require_once dirname(__FILE__).'/../../lib/urCronServiceTwitterUserTimeline.class.php';

clear_database();
$t = new lime_test(6, new lime_output_color());
$twitterFeed = new urCronFeed();
$twitterFeed->setName("my tweets");
$twitterFeed->setFeedUri('http://twitter.com/statuses/user_timeline/783214.rss');
$twitterFeed->setClass("urCronServiceTwitterUserTimeline");
$twitterFeed->save();

$twitterService = new urCronServiceTwitterUserTimeline();
$twitterService->setFeedId($twitterFeed->getId());
$twitterService->setFeedUri(file_get_contents($twitterFeed->getFeedUri()));

// ::getServiceName
$t->diag('::getServiceName()');
$t->is_deeply(urCronServiceTwitterUserTimeline::getServiceName(),
              array('Twitter user Timeline', 'urCronServiceTwitterUserTimeline'),
             '::getServiceName() returns an array with a description associated with the class name.');

$t->diag('->doCronJob()');
$twitterService->doCronJob();

$itemCount = count($twitterService->getFeed()->channel->item);

$items = Doctrine::getTable('urCronItem')->getItemsFromFeed($twitterFeed->getId());
$t->is($items->count(), $itemCount, "All items are imported into the database.");

$twitterService->doCronJob();
$items = Doctrine::getTable('urCronItem')->getItemsFromFeed($twitterFeed->getId());
$t->is($items->count(), $itemCount, "Items are not duplicated into the database.");

$item = $items->getFirst();
$t->is($item->getOriginalAuthor(), 'twitter', 'Original author is the right one.');
$t->is($item->getOriginalTitle(), $item->getOriginalContent(), 'Original title is the same as Original content');

$t->is(strpos($item->getTitle(), $item->getOriginalAuthor().':'), false, 'Item is cleaned before stored into database');